﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teriri.File.Client.Maui.Model;
using Teriri.File.UniversalLib.Helper;
using TouchSocket.Core;
using Microsoft.Maui.Storage;

namespace Teriri.File.Client.Maui.Helper
{
    public class ServerConfigHelper
    {
        public static TcpServerConfig Config;

        private static readonly object Lock = new object();

        /// <summary>
        /// 获取服务器配置
        /// </summary>
        public static async Task<TcpServerConfig> GetConfig()
        {
            lock (Lock)
            {
                if (Config != null)
                    return Config;
            }

            string iphost = await SecureStorage.Default.GetAsync("RemoteIPHost");
            string token = await SecureStorage.Default.GetAsync("VerifyToken");

            if (string.IsNullOrEmpty(iphost) || string.IsNullOrEmpty(token))
                return null;

            Config = new TcpServerConfig() { RemoteIPHost = iphost, VerifyToken = token };
            return Config;
        }

        public static async void SaveConfig(string iphost, string token)
        {
            Config = new TcpServerConfig() { RemoteIPHost = iphost, VerifyToken = token };

            await SecureStorage.Default.SetAsync("RemoteIPHost", iphost);
            await SecureStorage.Default.SetAsync("VerifyToken", token);
        }
    }
}
